/* Copyright (C) 2005-2013, Unigine Corp. All rights reserved.
 *
 * File:    fragment_base_shadow_world.h
 * Desc:    Base fragment shadow world shader
 * Version: 1.01
 * Author:  Alexander Zapryagaev <frustum@unigine.com>
 *
 * This file is part of the Unigine engine (http://unigine.com/).
 *
 * Your use and or redistribution of this software in source and / or
 * binary form, with or without modification, is subject to: (i) your
 * ongoing acceptance of and compliance with the terms and conditions of
 * the Unigine License Agreement; and (ii) your inclusion of this notice
 * in any version of this software that you use or redistribute.
 * A copy of the Unigine License Agreement is available by contacting
 * Unigine Corp. at http://unigine.com/
 */

#ifndef __FRAGMENT_BASE_SHADOW_WORLD_H__
#define __FRAGMENT_BASE_SHADOW_WORLD_H__

/******************************************************************************\
*
* OpenGL/OpenGLES
*
\******************************************************************************/

#ifdef OPENGL || OPENGLES

/*
 */
#ifdef SHADOW && (OPENGL || HAS_EXT_SHADOW_SAMPLERS)

/*
 */
#define SAMPLER_SHADOW_WORLD \
	uniform sampler2DShadow s_texture_7;

/*
 */
fixed shadowWorld(sampler2DShadow s_depth,half4 texcoord) {
	fixed shadow = 0.0f;
	half4 sample_texcoord = mul4(s_light_shadow_projections[0],texcoord);
	fixed2 sample_bounds = abs(sample_texcoord.xy - float2(0.25f,0.25f));
	if(max(sample_bounds.x,sample_bounds.y) > 0.245f) {
		sample_texcoord = mul4(s_light_shadow_projections[1],texcoord);
		sample_bounds = abs(sample_texcoord.xy - float2(0.75f,0.25f));
		if(max(sample_bounds.x,sample_bounds.y) > 0.245f) {
			sample_texcoord = mul4(s_light_shadow_projections[2],texcoord);
			sample_bounds = abs(sample_texcoord.xy - float2(0.25f,0.75f));
			if(max(sample_bounds.x,sample_bounds.y) > 0.245f) {
				sample_texcoord = mul4(s_light_shadow_projections[3],texcoord);
				sample_bounds = abs(sample_texcoord.xy - float2(0.75f,0.75f));
				if(max(sample_bounds.x,sample_bounds.y) > 0.245f) {
					shadow = 1.0f;
				}
			}
		}
	}
	if(shadow == 0.0f) {
		#ifdef OPENGL
			shadow = shadow2D(s_depth,sample_texcoord.xyz);
		#else
			shadow = shadow2DEXT(s_depth,sample_texcoord.xyz);
		#endif
		return saturate(shadow + s_light_shadow_iradius.y);
	}
	return 1.0f;
}

/*
 */
#define SHADOW_WORLD(COLOR) COLOR *= shadowWorld(s_texture_7,s_texcoord_4);

#else

/*
 */
#define SAMPLER_SHADOW_WORLD \
	uniform sampler2D s_texture_7;

/*
 */
#define SHADOW_WORLD(COLOR) { }

#endif

/******************************************************************************\
*
* Direct3D11/Direct3D119
*
\******************************************************************************/

#elif DIRECT3D11 || DIRECT3D119

/*
 */
#define TEXTURE_SHADOW_WORLD \
	Texture2D s_texture_7 : register(t7);

/*
 */
#ifdef SHADOW && HAS_FEATURE_LEVEL_10_0

/*
 */
half shadowWorld(Texture2D s_depth,SamplerComparisonState s_depth_sampler_compare,float4 texcoord) {
	half shadow = 0.0f;
	float4 sample_texcoord = mul4(s_light_shadow_projections[0],texcoord);
	half2 sample_bounds = abs(sample_texcoord.xy - half2(0.25f,0.25f));
	[flatten] if(max(sample_bounds.x,sample_bounds.y) > 0.245f) {
		sample_texcoord = mul4(s_light_shadow_projections[1],texcoord);
		sample_bounds = abs(sample_texcoord.xy - half2(0.75f,0.25f));
		[flatten] if(max(sample_bounds.x,sample_bounds.y) > 0.245f) {
			sample_texcoord = mul4(s_light_shadow_projections[2],texcoord);
			sample_bounds = abs(sample_texcoord.xy - half2(0.25f,0.75f));
			[flatten] if(max(sample_bounds.x,sample_bounds.y) > 0.245f) {
				sample_texcoord = mul4(s_light_shadow_projections[3],texcoord);
				sample_bounds = abs(sample_texcoord.xy - half2(0.75f,0.75f));
				[flatten] if(max(sample_bounds.x,sample_bounds.y) > 0.245f) {
					shadow = 1.0f;
				}
			}
		}
	}
	[flatten] if(shadow == 0.0f) {
		shadow = s_depth.SampleCmpLevelZero(s_depth_sampler_compare,sample_texcoord.xy,sample_texcoord.z);
		return saturate(shadow + s_light_shadow_iradius.y);
	}
	return 1.0f;
}

/*
 */
#define SHADOW_WORLD(COLOR) COLOR *= shadowWorld(s_texture_7,s_sampler_compare_7,IN.texcoord_4);

#else

/*
 */
#define SHADOW_WORLD(COLOR) { }

#endif

#endif

#endif /* __FRAGMENT_BASE_SHADOW_H__ */
